package com.atguigu.auth.controller;


import com.atguigu.common.config.exception.GuiguException;
import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.auth.service.SysMenuService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: sysLonigController
 * Package: com.atguigu.auth.controller
 * Description:
 *
 * @Author: lmf
 * @Create: 2024/1/20 13:10
 * @Version: 1.0
 */

@Api(tags = "登录")
@RestController // json
@RequestMapping("admin/system/login")
public class sysLoginController {
    private final SysUserService userService;

    private final SysMenuService menuService;

    public sysLoginController(SysUserService userService, SysMenuService menuService) {
        this.userService = userService;
        this.menuService = menuService;
    }





    @ApiOperation("用户登录")
    @PostMapping("login")
    public Result<Object> login(@RequestBody LoginVo loginVo) {
        // 查询用户表对应用户名称+status = 1数据
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, loginVo.getUsername());
        queryWrapper.eq(SysUser::getStatus, 1);

        SysUser sysUser = userService.getOne(queryWrapper);

        if (sysUser == null) {
            throw new GuiguException(201, "暂无对应用户");
        }

        if (!sysUser.getPassword().equals(MD5.encrypt(loginVo.getPassword()))) {
            throw new GuiguException(201, "密码不正确");
        }

        String username = loginVo.getUsername();
        String token = JwtHelper.createToken(sysUser.getId(), username);
        HashMap<String, Object> data = new HashMap<>();
        data.put("token", token);
        return Result.ok(data);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("info")
    public Result<Object> info(HttpServletRequest request) {
        String token = request.getHeader("token");
        // 将token 中id获取出来
        final Long userId = JwtHelper.getUserId(token);
        //根据id获取用户可操作菜单列表
        final SysUser sysUser = userService.getById(userId);

        // 查询数据库构建路由结构进行显示
        List<RouterVo> routerVoList = menuService.findUserMenuListByUserId(userId);

        // 根据用户id获取用户可操作菜单列表
        List<String> permsList = menuService.findUserPermsByUserId(userId);

        // 返回对应数据
        final HashMap<String, Object> data = new HashMap<>();
        data.put("name", sysUser.getName());
        data.put("routers", routerVoList);
        data.put("buttons", permsList);
        return Result.ok(data);
    }
}
